using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TVT.Common.Exceptions;
using System.Threading;
using System.Collections.Generic;
using TVT.Common.Helpers;
using TVT.UI.Helpers;
using TVT.BizLayer.Entities;

namespace TVT.UI.UserControls
{
    public partial class CreateUpdateCompany : System.Web.UI.UserControl
    {
        public event EventHandler OnSavedCompany = null;

        private void SavedCompany(EventArgs args)
        {
            if (OnSavedCompany != null)
            {
                OnSavedCompany(this, args);
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        public void EditCompany(int idCompany)
        {
            Company company = Company.Find(idCompany);
            txtCompanyName.Text = company.Name;
            txtAccountMFee.Text = string.Format(Resources.Decimal4Format,company.AccountManagmentFee.ToString());
            txtTelephone.Text = company.Telephone;
            txtAddress.Text = company.Address;
            txtTaxId.Text = company.TaxId;
            txtPerfFee.Text = string.Format(Resources.Decimal4Format, company.PerfFee.ToString());
            ViewState[WellKnownKeys.IdCompanyKey] = idCompany;
            upCreateUpdateCompany.Update();
            
        }

        protected void SaveCompany(object sender, EventArgs e)
        {                              
            Company company = new Company();
            company.Name = txtCompanyName.Text;
            company.AccountManagmentFee = Decimal.Parse(txtAccountMFee.Text);
            company.PerfFee = Decimal.Parse(txtPerfFee.Text);
            company.Telephone = txtTelephone.Text;
            company.Address = txtAddress.Text;
            company.TaxId = txtTaxId.Text;
            bool isNew = false;

            if (ViewState[WellKnownKeys.IdCompanyKey] != null)
            {
                company.Id = Int32.Parse(ViewState[WellKnownKeys.IdCompanyKey].ToString());
            }
            else
            {
                company.Id = 0;
            }

            if (company.Id == 0)
                isNew = true;

            Company.Save(isNew, (Guid)Membership.GetUser().ProviderUserKey,company);
            SavedCompany(new EventArgs());
            
            lblTransactionResultMessage.Visible = true;
            if (isNew)
                lblTransactionResultMessage.Text = Resources.SuccesfullMessage + Resources.RemainderDataBaseActualization;
            else
                lblTransactionResultMessage.Text = Resources.SuccesfullMessage;

            CancelCompany(this, e);            
        }

        
        protected void CancelCompany(object sender, EventArgs e)
        {
            CleanControls();
            ViewState[WellKnownKeys.IdCompanyKey] = 0;
            upCreateUpdateCompany.Update();
        }

        public void CancelCompany()
        {
            CancelCompany(this, new EventArgs());
        }

        private void CleanControls()
        {
            txtCompanyName.Text = "";
            txtAccountMFee.Text = "";
            txtTelephone.Text = "";
            txtAddress.Text = "";
            txtTaxId.Text = "";
            txtPerfFee.Text = "";
            lblTransactionResultMessage.Visible = false;
        }
    }
}